package cn.edu.nchu.fragmentdemo.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.fragment.app.Fragment;

import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.entity.MyMarkerEntity;
import cn.edu.nchu.fragmentdemo.myComponents.overLays.RouteLay;
import cn.edu.nchu.fragmentdemo.utils.MyOverlayManager;



/**
 * @author Tsuki
 * @description: 绘制航线时用到的功能栏
 */
public class UavRouteTaskFunctionBarFragment extends Fragment {

    public ImageButton mTaskIbtnPlay;
    public ImageButton mTaskIbtnStop;
    public ImageButton mTaskIbtnReverse;
    public ImageButton mTaskIbtnRemove;
    public ImageButton mTaskIbtnDelete;
    public ImageButton mTaskIbtnSave;
    public ImageButton mTaskIbtnUpload;
    RouteLay mCurrentRouteLay;
    /*ListTaskWaypointAdapter pointAdapter;
    GridTaskWaypointAdapter gridpointAdapter;*/

    private MyMarkerEntity mymarkerEntity = new MyMarkerEntity();

    @SuppressLint("ResourceType")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragement_route_task, container, false);
        mymarkerEntity.setId("");
        //添加图标点击监听
        mTaskIbtnPlay = rootView.findViewById(R.id.mTaskIbtnPlay);
        mTaskIbtnStop = rootView.findViewById(R.id.mTaskIbtnStop);
        mTaskIbtnReverse = rootView.findViewById(R.id.mTaskIbtnReverse);
        mTaskIbtnRemove = rootView.findViewById(R.id.mTaskIbtRemoveAll);
        mTaskIbtnDelete = rootView.findViewById(R.id.mTaskIbtnDelete);
        mTaskIbtnSave = rootView.findViewById(R.id.mTaskIbtnSave);
        mTaskIbtnUpload = rootView.findViewById(R.id.mTaskIbtnUpload);

        mTaskIbtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTaskIbtnStop.setEnabled(true);
                mTaskIbtnPlay.setEnabled(false);
                //添加新的航线图层
                MyOverlayManager.getInstance().addMyOverlay("POLYLINE",mymarkerEntity.getId());
            }
        });
        mTaskIbtnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mTaskIbtnStop.setEnabled(false);
                mTaskIbtnPlay.setEnabled(true);
                //用于polyline和polygon的结束绘制，同时在全局图层管理里保存
                MyOverlayManager.getInstance().removeMyOverlay("POLYLINE");
                //判断当前marker点的mCurrentRouteLay是否为空，不空显示出来；
                try {
                    Thread.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mCurrentRouteLay = MyOverlayManager.getInstance().getPolylineKeyUK(mymarkerEntity.getId());
                /*if (mCurrentRouteLay != null) {
                    mymarkerEntity.setWhichRouteUK(mCurrentRouteLay.getMyRouteEntity().getId());
                    //设置
                    mTaskRoutepointNum.setText(mCurrentRouteLay.getMarkers().size() + "");
                    ////
                    pointAdapter = new ListTaskWaypointAdapter(mainActivity,
                            mCurrentRouteLay.getMarkers(), mCurrentRouteLay.getGeoPoints(), mMapView);
                    mTaskLvWaypoint.setAdapter(pointAdapter);
                    mCurrentRouteLay.setDataChangeListener(new RouteLay.DataChangeListener() {
                        @Override
                        public void setData(Object result) throws SQLException {
                            if (result.toString().equals("change")) {
                                mTaskLvWaypoint.setAdapter(pointAdapter);
                            }
                            if (result.toString().equals("change78")) {
                                mTaskLvWaypoint.setAdapter(pointAdapter);
                                mMapView.getOverlays().add(mCurrentRouteLay.getMyArrowMarkerClass());
                                mTaskRoutepointEplinearlayout.setVisibility(View.VISIBLE);
                                mMapView.invalidate();

                            }
                            if (result.toString().equals("resume78")) {
                                mTaskLvWaypoint.setAdapter(pointAdapter);
                                mMapView.getOverlays().remove(mCurrentRouteLay.getMyArrowMarkerClass());
                                mTaskRoutepointEplinearlayout.setVisibility(View.GONE);
                                mMapView.invalidate();
                            }
                            mTaskRoutepointNum.setText(mCurrentRouteLay.getMarkers().size() + "");
                        }
                    });
                }*/
            }
        });
        mTaskIbtnReverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentRouteLay = MyOverlayManager.getInstance().getPolylineKeyUK(mymarkerEntity.getId());
                mCurrentRouteLay.reverseRouteInMap();
            }
        });
        mTaskIbtnRemove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentRouteLay = MyOverlayManager.getInstance().getPolylineKeyUK(mymarkerEntity.getId());
                mCurrentRouteLay.romoveAllinMap();
            }
        });


        return rootView;
    }

}
